package com.bingxue.edu.common.job;

import com.bingxue.edu.common.enums.OrderStatusEnum;
import com.bingxue.edu.management.shop.model.entity.Order;
import com.bingxue.edu.management.shop.service.OrderService;
import com.bingxue.edu.framework.job.DynamicJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.List;

import static com.bingxue.edu.management.shop.model.entity.table.OrderTableDef.ORDER;

@Slf4j
@DisallowConcurrentExecution
@RequiredArgsConstructor
@DynamicJob(name="线上支付结果查询任务", cron = "0 */10 * * * ? ", paramClass = DemoJobParam.class)
public class OnlinePayRefreshJob extends QuartzJobBean {

    private final OrderService orderService;

    @Override
    protected void executeInternal(JobExecutionContext context) {
        List<Order> pendingOrderList = orderService.queryChain()
                .where(ORDER.STATUS.eq(OrderStatusEnum.PENDING))
                .list();
        for (Order order : pendingOrderList) {
            try {
                orderService.refreshPayResult(order);
            } catch (Exception e) {
                log.warn("[定时任务]线上支付结果查询任务失败，orderId: {}, {}", order.getId(), e.getMessage());
            }
        }
    }
}
